package com.liusand.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liusand.admin.mapper.BlogMapper;
import com.liusand.admin.mapper.CategoryMapper;
import com.liusand.admin.service.BlogService;
import com.liusand.xo.pojo.Blog;
import com.liusand.xo.pojo.Category;
import com.liusand.xo.vo.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author LiuSha
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Blog blog) {
        Blog blogInDb = findByTitle(blog.getTitle());
        if(blogInDb != null){
            return false;
        }
        return blogMapper.insert(blog) > 0;
    }

    @Override
    public boolean deleteById(String id) {
        Blog blog = findById(id);
        if(blog == null){
            return false;
        }
        blog.setDeleted(LocalDateTime.now());
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, id);
        blogMapper.update(blog, wrapper);
        return blogMapper.update(blog, wrapper) > 0;
    }

    @Override
    public boolean update(Blog blog) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId, blog.getBlogId());

        blog.setUpdated(LocalDateTime.now());
        Blog blogInDb = findByTitle(blog.getTitle());
        if(blogInDb != null && !blogInDb.getBlogId().equals(blog.getBlogId())){
            return false;
        }
        blog.setAuthor(null);
        return blogMapper.update(blog, wrapper) > 0;
    }

    @Override
    public List<BlogVO> findAll() {
        return blogMapper.findAllVO();
    }

    public Blog findById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        return blogMapper.selectOne(wrapper);
    }

    @Override
    public BlogVO findVOById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        Blog blog = blogMapper.selectOne(wrapper);
        if(blog == null){
            return null;
        }
        Category category = categoryMapper.selectById(blog.getCategoryId());
        return new BlogVO(blog.getBlogId(), category.getCategoryName(), blog.getTitle(), blog.getAuthor(), blog.getCreated());
    }

    @Override
    public BlogVO findVOByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle,title);
        Blog blog = blogMapper.selectOne(wrapper);
        if(blog == null){
            return null;
        }
        Category category = categoryMapper.selectById(blog.getCategoryId());
        return new BlogVO(blog.getBlogId(), category.getCategoryName(), blog.getTitle(), blog.getAuthor(), blog.getCreated());
    }

    @Override
    public Blog findByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);
        return blogMapper.selectOne(wrapper);
    }
}
